473,465 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Restricting access of class variable to another single class?

Hi all,

The next few paragraphs put my question in context, but feel free to
skip down to the end if you don't care *why* I need the answer! :)

****************************************
*** Start of long-winded description ***
****************************************

I've got some classes that define a tree structure. When the Parent
property of an item is changed, it automatically updates the Children list.

The opposite is also true - if you add an item to a Children list, it's
parent is automatically updated.

The way this second part works, is with an event (fired when items are
added to the list) like this:

children.Added += delegate(object sender,
ChildList<T>.ChildListEventArgs<T> e) { e.Item.parent = (T)this; };

Notice the lowercase p in parent. If I change this to a P, then I'll get
a StackOverflow, because setting the parent, adds to the child list, and
adding to the child list sets the parent. This works fine, because
everything is in one class (the tree is a tree of one type).

Now, I've extended it slightly to support ClassA having children of
ClassB, and ClassB has a parent of ClassA.

My problem, is that from within ClassB, when the child list is modified,
I need to set the parent of ClassA. My current method requires that the
parentClassB property within ClassA be internal (if I use the public
property, I'll overflow the stack again). However, it's important people
(me) don't "accidentally" use this variable when meaning to use the
public property (else my magic code won't get called, undermining the
whole point of me abstracting the tree stuff!).

****************************************
*** End of long-winded description ***
****************************************

So... In short - is there any way to restrict access, like "internal",
but only to subset of classes within the DLL?

If not, the only way to make sure my code is used properly, is to move
ClassA and ClassB into a separate DLL, so only they can set each others
internal data.

The problem with that, is that ClassA and ClassB then need to be public,
and that means anyone can reference my DLL and use my classes, whereas
if everything is in one, they can be internal classes.
Are there any solutions? :-(
Nov 30 '05 #1
6 1752
> So... In short - is there any way to restrict access, like "internal",
but only to subset of classes within the DLL?


What you're after is something like the "friend" designation available
in some other languages. C# doesn't have it, so no.

That said, one way to at least reduce confusion is to expose the
internal "set parent without fiddling with parent's child list"
operation as a method, rather than a property (or, even worse, exposing
the field directly). For example, you could have:

public ClassB Parent
{
get { return this._parent; }
set
{
SetParent(value);
... update parent's children ...
}
}

internal void SetParent(ClassB newParent)
{
this._parent = newParent;
}

Then at least the code within your DLL would know that if it's setting
Parent it's getting the public version, while to do an internal set
(with no side effects) it has to call the method, and the difference
will be more visible (rather than being nothing more than a case
difference: parent versus Parent).

Nov 30 '05 #2
Bruce Wood wrote:
That said, one way to at least reduce confusion is to expose the
internal "set parent without fiddling with parent's child list"
operation as a method, rather than a property (or, even worse, exposing
the field directly). For example, you could have:

public ClassB Parent
{
get { return this._parent; }
set
{
SetParent(value);
... update parent's children ...
}
}

internal void SetParent(ClassB newParent)
{
this._parent = newParent;
}

Then at least the code within your DLL would know that if it's setting
Parent it's getting the public version, while to do an internal set
(with no side effects) it has to call the method, and the difference
will be more visible (rather than being nothing more than a case
difference: parent versus Parent).


I thought about that too (it stops intellisense picking the wrong one
parent/Parent), though it's not as nice as I'd hoped. I'll go with it
for now (since it's the best solution), but I think my whole tree thing
is a bit messy now, so might re-plan it a bit better at some point!
Nov 30 '05 #3
You can gain knowledge about who is calling your method by using the StackTrace
class as shown below.
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(1);
MethodBase methodBase = stackFrame.GetMethod();
Type callingType=methodBase.DeclaringType;

stackTrace.GetFrame(1) gets the immediate caller of your method. You can
then get the type declaring the method that made the invocation by inspecting
the DeclaringType property of the MethodBase class. You can use this information
to restrict calls to a method by type.

A word of caution is due; inspecting the stack trace can be slow.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 30 '05 #4
Incidentally, that's why I started using the _xxx notation for private
member fields. I hate stupid prefixes, but boy, does it make it easier
to use Intellisense. No more picking the field instead of the property,
or vice versa.

It also has the advantage that you see the fields first in the
debugger's Local window, so you avoid the endless slow scrolling as the
debugger evaluates all of the properties from A to O just to get to the
"parent" field.

Dec 1 '05 #5
Bruce Wood wrote:
Incidentally, that's why I started using the _xxx notation for private
member fields. I hate stupid prefixes, but boy, does it make it easier
to use Intellisense. No more picking the field instead of the property,
or vice versa.

It also has the advantage that you see the fields first in the
debugger's Local window, so you avoid the endless slow scrolling as the
debugger evaluates all of the properties from A to O just to get to the
"parent" field.


heh, I've used _xxx a few times, but it's so ugly, I always go back to
lower-case for fields and ProperCase(!) for properties. An addition to
your reasons, my reason was so that in a method with its own local
variables, it's easy to distinguish what's "object-scoped", and won't
disappear when this method ends! :)
Dec 1 '05 #6
Anders Norås wrote:
You can gain knowledge about who is calling your method by using the
StackTrace class as shown below.

<snip>

I thought about that (after the response in the EventHandling thread),
but it's a little nasty. I think I'll use a method to set it for now, so
it's not going to slow things down, or leave an ugly field visible
externally!
Dec 1 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Gaurav Veda | last post by:
Hi ! I am a poor mortal who has become terrified of Python. It seems to have thrown all the OO concepts out of the window. Penniless, I ask a basic question : What is the difference between a...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
49
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
4
by: Dennis C. Drumm | last post by:
Is there a way with C# to allow one class access to a method or field of another class, without making that method or field visible to all other classes, as would be the case when making the method...
4
by: Danny Tuppeny | last post by:
Hi all, The next few paragraphs put my question in context, but feel free to skip down to the end if you don't care *why* I need the answer! :) **************************************** ***...
1
by: ward | last post by:
Good morning everyone. I'm building a very simple content management site that tracks "tasks." The options available are: 1. Add Task 2. Edit Task 3. View Task 4. Print Task
9
by: LamSoft | last post by:
Class B { public B() {} } Class A : B { public static string ABC = "myABC"; public A() {} }
2
by: sant.tarun | last post by:
Hi, I am facing some some problem in restricting the access of a variable.... My question is described below..... Let I have two different C source files 'a.c' and 'b.c'. In the file 'a.c'...
4
by: Christopher | last post by:
I am surprised this hasn't come up for me more in the past, but the situation is: I need to have an interface that is usable for all I need to have an interface that is only usable for some I...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.